package com.atguigu.dga.governance.assessor.spec;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 是否有字段备注
 * 有备注字段/所有字段 *10分
 * @Author: lay
 * @Date: 2024/6/28 14:27
 */
@Component("TABLE_COL_COMMENT")
public class TableColCommentAssessor extends Assessor {
    @Override
    public void checkProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) {
        String colNameJson = assessParam.getTableMetaInfo().getColNameJson();
        List<JSONObject> jsonObjectList = JSON.parseArray(colNameJson, JSONObject.class);
        List<String> list = new ArrayList<>();
        for (JSONObject jsonObject : jsonObjectList) {
            String comment = jsonObject.getString("comment");
            if (comment == null || comment.trim().isEmpty()){
                list.add(jsonObject.getString("name"));
            }
        }
        if (list.size() > 0){
            governanceAssessDetail.setAssessScore(BigDecimal.valueOf((jsonObjectList.size() - list.size()) * 10L / jsonObjectList.size()));
            governanceAssessDetail.setAssessProblem("未设置字段备注");
            governanceAssessDetail.setAssessComment("字段为：" + list);
        }
    }
}
